package com.izhang3.proxy.client;

import com.izhang3.proxy.channel.ChannelService;
import com.izhang3.proxy.client.proxy.KuaiDaiLiParser;
import com.izhang3.proxy.client.proxy.Site66IpParser;
import com.izhang3.proxy.client.proxy.Site89IpParser;
import com.izhang3.proxy.client.proxy.XiCiParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.concurrent.*;

/**
 * @author 张良
 * @desc
 * @since 2019/6/20 11:30
 */
@Component
public class CrawlClientService {

    @Autowired
    private ChannelService channelService;

    private static Logger logger = LoggerFactory.getLogger(CrawlClientService.class);

    /**
     * 最大线程数
     */
    private static final int MAX_THREAD_COUNT = 4;

    private static ExecutorService executorService =
            Executors.newFixedThreadPool(MAX_THREAD_COUNT);

    /**
     * 执行爬取
     */
    public void pull() {
        try {
            //使用java并发库concurrent
            CountDownLatch latch = new CountDownLatch(MAX_THREAD_COUNT);
            //爬取快代理
            executorService.submit(new CrawlClientPool(new KuaiDaiLiParser(), channelService, latch));
            //爬取89IP
            executorService.submit(new CrawlClientPool(new Site89IpParser(), channelService, latch));
            //爬取66代理
            executorService.submit(new CrawlClientPool(new Site66IpParser(), channelService, latch));
            //爬取西次代理
            executorService.submit(new CrawlClientPool(new XiCiParser(), channelService, latch));
            //阻塞当前线程直到latch中数值为零才执行
            latch.await();
        } catch (Exception e) {
            logger.error("执行异常", e);
        }
    }
}
